<?php
class CAmOrder extends Zend_Db_Table{
	protected $_name;
	
	protected $_primary;
	
	public function __construct(){
	
		$this->_name=R_PRE."am_order";
	
		$this->_primary="orderid";
	
		parent::__construct();
	
	}
	/*public function insert($data){
		$id=$this->insert($data);
		
		return $id;
	}*/
	public function getAmOrderDetail($orderid){
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name);
		
		$select->where('orderid = ? ',$orderid);
		
		return $db->fetchRow($select);
	}
	public function updateStatus($orderid,$iscomplete){
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('orderid = ?', $orderid);
		
		$data = array('iscomplete'=>$iscomplete);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
				
			return false;
				
		}else{
				
			return true;
				
		}
	}
	public function getAllOrders($uid,$page=1,$pagenum=10){
		$code = $this->_getCodeByUid($uid);
		if(!empty($code['relatecode'])){
			$db = $this->getAdapter();
			$start=$pagenum*($page-1);
			$sql ="select * from zcshop_am_order o 
			left join zcshop_orders s on o.orderid=s.orderid
			left join zcshop_members m on m.uid=s.uid 
			where o.relatecode='{$code['relatecode']}' and s.flid in (2,3,4,5,9) limit $start,$pagenum";
			$rows = $db->fetchAll($sql);
			$rows['counts']=count($rows);
			return $rows;
			
		}
	}
	public function _getCodeByUid($uid){
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from('zcshop_am_member');
		$select->where('uid = ? ',$uid);
		return $db->fetchRow($select);
	}
	public function getAllOrdersCount($uid){
		$code = $this->_getCodeByUid($uid);
		if(!empty($code['relatecode'])){
			$db = $this->getAdapter();
			$sql ="select * from zcshop_am_order o
			left join zcshop_orders s on o.orderid=s.orderid
			left join zcshop_members m on m.uid=s.uid
			where o.relatecode='{$code['relatecode']}' and s.flid in (2,3,4,5,9)";
			$rows = $db->fetchAll($sql);
			return count($rows);
				
		}
		}
	
	

}